#!/bin/sh

for DIR in */root; do
	PACKAGE=${DIR/\/root/}
	rm -f /tmp/$PACKAGE.filelist /tmp/$PACKAGE.deps
	find $DIR -type f | while read FILE; do
		IN_ENTRIES=0
		otool -L "$FILE" 2>/dev/null | grep -v 'is not an object' | while read ENTRY; do
			if [ $IN_ENTRIES -eq 0 ]; then
				echo $FILE
				IN_ENTRIES=1
				echo $ENTRY | awk '{ print $1 }' | sed -e 's,:$,,' -e "s,^${DIR},," >>/tmp/$PACKAGE.filelist.tmp
			else
				echo $ENTRY | awk '{ print $1 }' >>/tmp/$PACKAGE.deps.tmp
			fi
		done
	done

	if [ -f /tmp/$PACKAGE.filelist.tmp ]; then
		sort -u /tmp/$PACKAGE.filelist.tmp > /tmp/$PACKAGE.filelist
	fi
	rm -f /tmp/$PACKAGE.filelist.tmp
	if [ -f /tmp/$PACKAGE.deps.tmp ]; then
		sort -u /tmp/$PACKAGE.deps.tmp > /tmp/$PACKAGE.deps
	fi
	rm -f /tmp/$PACKAGE.deps.tmp
done
